package simi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ModelFileManager {

	private static final String JSP_EXT = ".jsp";
	private static String APP_DIR = "/simi";

	public static void copyGlobalModelFiles(String strPath, String dstPath)
			throws IOException {

		File src = new File(strPath);
		File dest = new File(dstPath);

		if (src.isDirectory()) {
			dest.mkdirs();
			String list[] = src.list();

			for (int i = 0; i < list.length; i++) {
				String dest1 = dest.getAbsolutePath() + "/" + list[i];
				String src1 = src.getAbsolutePath() + "/" + list[i];
				File theModel = new File(src1);
				File theNewModel = new File(dest1);
				if (theModel.getName().toLowerCase().endsWith(JSP_EXT)) {
					copy(theModel, theNewModel);
				}
			}
		}
	}

	public static void copyUserModelFiles(String strPath, String dstPath)
			throws IOException {

		File src = new File(strPath);
		File dest = new File(dstPath);

		if (src.isDirectory()) {
			dest.mkdirs();
			String list[] = src.list();

			for (int i = 0; i < list.length; i++) {
				String dest1 = dest.getAbsolutePath() + "/" + list[i];
				String src1 = src.getAbsolutePath() + "/" + list[i];
				copyUserModelFiles(src1, dest1);
			}
		} else {
			if (dest.getName().toLowerCase().endsWith(JSP_EXT)) {
				copy(src, dest);
			}
		}
	}

	// Deletes all files and subdirectories under dir.
	// Returns true if all deletions were successful.
	// If a deletion fails, the method stops attempting to delete and returns
	// false.
	public static boolean deleteModelFiles(File dir) {

		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteModelFiles(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// The directory is now empty so delete it
		return dir.delete();
	}

	/**
	 * Recursively walk a directory tree and return a List of all Models found;
	 * the List is sorted using Model.compareTo.
	 * 
	 * @param aStartingDir
	 *            is a valid directory, which can be read.
	 */

	@SuppressWarnings("unchecked")
	// unhappiness...
	public static List<Model> getModels(File modelDir) {

		List<Model> result = new ArrayList<Model>();
		if (modelDir.exists()) {
			File[] filesAndDirs = modelDir.listFiles();
			List<File> filesDirs = Arrays.asList(filesAndDirs);
			for (File file : filesDirs) {
				if (!file.isFile()) {
					// must be a directory, recursive call
					List<Model> deeperList = getModels(file);
					result.addAll(deeperList);
				} else {
					result.add(getModelFromFile(file));
				}
			}
			Collections.sort(result);
		}
		return result;
	}

	private static Model getModelFromFile(File theFile) {
		Model theModel = new Model();
		String theName = theFile.getName();
		theModel.setName(theName.substring(0, theName.indexOf(JSP_EXT)));
		String thePath = theFile.getAbsolutePath();
		theModel.setLink(thePath.substring(thePath.indexOf(APP_DIR)));
		return theModel;
	}

	private static void copy(File src, File dest) throws IOException {

		FileInputStream fin = null;
		FileOutputStream fout = null;
		try {
			fin = new FileInputStream(src);
			fout = new FileOutputStream(dest);
			int c;
			while ((c = fin.read()) >= 0)
				fout.write(c);
		} finally {
			fin.close();
			fout.close();
		}
	}
}